package com.chujian.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chujian.dao.OrderDao;
import com.chujian.daoImpl.OrderDaoImpl;
import com.chujian.jpush.Jdpush;
import com.chujian.model.NotificationBean;
import com.google.gson.Gson;

/**
 * 
 * 实现登录的功能
 * @author 周映凯
 */
@WebServlet("/ChangeTableServlet")
public class ChangeTableServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangeTableServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String orderId=request.getParameter("orderId");
		String tableName=request.getParameter("tableName");
		tableName=new String(tableName.getBytes("iso-8859-1"),"UTF-8");//解决中文乱码
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		System.out.println(tableName);
		OrderDao dao=new OrderDaoImpl();
		if(orderId!=null){
			boolean result=dao.modifyTable(orderId, tableName);
			if(result){
				Gson gson=new Gson();
				String notice=gson.toJson(new NotificationBean("0", "用户换桌提醒", "订单号："+orderId+"将餐桌更换到了"+tableName));
				Jdpush.sendPushAlias(notice, "用户换桌提醒", "admin");
				out.write("changeTableSuccess");
			}else {
				out.write("changeTableFailed");
			}
		}
		
		
		
	}

}
